<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/1/11
 * Time: 18:02
 * 模型
 */

namespace app\model;

use app\lib\db\DB;
class Model
{
    protected $db;
    protected $table;
    protected $redis;
    public function __construct(DB $db,$table,\Redis $redis=null)
    {
        $this->db = $db;
        $this->table = $table;
        $this->redis = $redis;
    }
    public function setDb($db){
        $this->db = $db;
    }
    public function setRedis(\Redis $redis){
        $this->redis = $redis;
    }

    public function setTable($table)
    {
        $this->table = $table;
    }
    public function save($type,array $data,array $condition = []){
        if(!$this->table){
            return false;
        }
        if($type == "add"){
            return $this->db->insert($this->table,$data);
        }else{
            if(!$condition){
                return false;
            }
            return $this->db->update($this->table,$data,$condition);
        }
    }
    public function getInfo($where,$select="*"){
        $this->db->select($select);
        $this->db->from($this->table);
        $this->db->where($where);
        $query = $this->db->get();
        return $query->one();
    }
    public function getCount(array $where)
    {
        $this->db->from($this->table);
        $this->db->where($where);
        return $this->db->count_all_results(true);
    }

    /**
     * @desc 单表查询
     * @param array $config 配置
     * @param bool $retCount
     * @return array
     */
    public function getList(array $config=[],$retCount=false)
    {
        $count = 0;
        $data = null;
        if(!empty($config["select"])){
            $this->db->select($config["select"]);
        }
        $this->db->from($this->table);
        if(!empty($config["eq"])){
            $this->db->where($config["eq"]);
        }
        if(!empty($config["eq-filter"])){
            $this->db->where(array_filter($config["eq-filter"]));
        }
        if(!empty($config["group"])){
            //多个group by用逗号分割
            $this->db->group_by($config["group"]);
        }
        if($retCount){
            $count =  $this->db->count_all_results();
        }
        if(!empty($config["page"])){
            //分页
            $this->db->limit($config["page"]["size"],($config["page"]["p"]-1)*$config["page"]["size"]);
        }
        if(!empty($config["order"])){
            $this->db->order_by($config["order"]);
        }
        $query = $this->db->get();
        $data = $query->all();
//        echo $this->db->last_query();exit;
        if($retCount){
            return ["total"=>(int)$count,"data"=>$data];
        }else{
            return $data;
        }
    }

    /**
     * 获取分类
     */
    public function type(){
        $this->db->select("id,name");
        $this->db->from("type");
        $this->db->where(["online"=>2,"is_other!="=>999999999]);
        $total =  $this->db->count_all_results();
        $this->db->order_by(["sort"=>"asc","add_time"=>"desc"]);
        $query =  $this->db->get();
        $datas = $query->all();
        $data["total"] = $total;
        $data["data"] = $datas;
//        echo $this->db->last_query();exit;
        return $data;
    }

    /**
     * 获取话题
     */
    public function type_sub($type=1){
        if($type==1){
            $this->db->select("*");
        }else if($type==2){
            $this->db->select("id,sub_title");
        }else if($type==4){
            $this->db->select("id,sub_title");
        }else{
            $this->db->select("*");
        }
        $this->db->from("type_sub");
        if($type==1){
            $this->db->where(["online"=>2]);
        }else if($type==3){
            $this->db->where(["is_other !="=>999999999]);
        }else if($type==4){
            $this->db->where(["online"=>2,"is_other !="=>999999999]);
        }
        $query =  $this->db->get();
        $datas = $query->all();
//        echo $this->db->last_query();exit;
        return $datas;
    }

    /**
     * 查询单条数据
     */
    public function one($table,$where,$select=""){
        if(empty($select)){
            $this->db->select("*");
        }else{
            $this->db->select($select);
        }
        $this->db->from($table);
        $this->db->where($where);
        $query =  $this->db->get();
        $datas = $query->one();
        return $datas;
    }
    /**
     * 查询单条数据
     */
    public function all($table,$where,$select=""){
        if(empty($select)){
            $this->db->select("*");
        }else{
            $this->db->select($select);
        }
        $this->db->from($table);
        $this->db->where($where);
        $query =  $this->db->get();
        $datas = $query->all();
        return $datas;
    }
}
